package com.wl.shop.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wl.shop.dao.UserRoleDao;
import com.wl.shop.entity.UserRole;
import com.wl.shop.service.UserRoleService;

@Service("userRoleService")
@Transactional(readOnly = true)
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRole> implements UserRoleService {

	@Override
	public void saveUserRole(String userId, String roleIds) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotBlank(roleIds)) {
			List<UserRole> list = Lists.newArrayList();
			String[] roleIdList = roleIds.split(",");
			for (String roleId : roleIdList) {
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId);
                userRole.setUserId(userId);
                list.add(userRole);
            }
			Map<String, Object> map = Maps.newHashMap();
			map.put("user_id", userId);
			deleteByMap(map);
			insertBatch(list);
		}
	}

	@Override
	public List<String> getRoleIdsByUserId(String userId) {
		// TODO Auto-generated method stub
		EntityWrapper<UserRole> wrapper = new EntityWrapper<UserRole>();
		wrapper.eq("user_id", userId);
		List<UserRole> userRoles = baseMapper.selectList(wrapper);
		List<String> roleIds = Lists.newArrayList();
		for (UserRole userRole : userRoles) {
			roleIds.add(userRole.getRoleId());
		}
		return roleIds;
	}

}
